package com.cheems.service.impl;

import com.cheems.dao.PicMapper;
import com.cheems.entity.DTO.picDTO;
import com.cheems.entity.DTO.picUpdateDTO;
import com.cheems.entity.DTO.wallListDTO;
import com.cheems.entity.Pojo.pic;
import com.cheems.entity.Result.PageResult;
import com.cheems.service.PicService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PicImpl implements PicService {
    @Autowired
    private PicMapper picMapper;
    /**
     * 分类中壁纸详情
     *
     * @param wallListDTO
     * @return
     */
    @Override
    public PageResult wallList(wallListDTO wallListDTO) {
        //开始分页查询
        //select * from 表 limit 0,10
        PageHelper.startPage(wallListDTO.getPageNum(),wallListDTO.getPageSize());
        //mapper返回对象
        Page<pic> page = picMapper.pageQuery(wallListDTO);
        //封装pageResult对象
        long total = page.getTotal();
        List<pic> result = page.getResult();

        return new PageResult(total,result);
    }

    /**
     * 随机获取图片
     * @return
     */
    @Override
    public List<pic> randomWall() {
        List<pic> picList = picMapper.randomWall();
        return picList;
    }

    /**
     * 获取图片详情
     * @param id
     * @return
     */
    @Override
    public pic detailWall(Integer id) {
        pic pic = picMapper.detailWall(id);
        return pic;
    }

    /**
     * 新增壁纸
     */
    @Override
    public void addPic(picDTO picDTO) {
        //根据DTO更新
        //创建pic对象
        pic pic = new pic();
        //复制picDTO的属性
        BeanUtils.copyProperties(picDTO,pic);
        //对pic空余的属性赋值
        pic.setCreateTime(String.valueOf(System.currentTimeMillis()));
        pic.setUpdateTime(String.valueOf(System.currentTimeMillis()));
        picMapper.addPic(pic);
    }

    /**
     * 修改壁纸
     * @param
     */
    @Override
    public void updatePic(picUpdateDTO picUpdateDTO) {
        //根据DTO更新
        //创建pic对象
        pic pic = new pic();
        //复制picDTO的属性
        BeanUtils.copyProperties(picUpdateDTO,pic);
        //对pic空余的属性赋值
        pic.setUpdateTime(String.valueOf(System.currentTimeMillis()));
        picMapper.updatePic(pic);
    }

    /**
     * 删除图片
     * @param id
     */
    @Override
    public void delPic(Integer id) {
        picMapper.delPic(id);
    }
}
